package com.sjd.mall.service.account.impl;


import cn.hutool.core.util.ObjectUtil;
import com.sjd.common.constant.MemberLevelConstants;
import com.sjd.mall.domain.SjdAccountDO;
import com.sjd.mall.mapper.SjdAccountMapper;
import com.sjd.mall.service.account.AccountService;
import com.sjd.mall.mapper.SjdAccountGlodenBeanDetailsMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ProjectName: sjd
 * @Package: com.sjd.protal.account.service.impl
 * @ClassName: AccountServiceImpl
 * @Author: genglong
 * @Description: 账户实现类
 * @Date: 2022/1/11 10:26
 * @Version: 1.0
 */
@Service
@AllArgsConstructor
public class AccountServiceImpl implements AccountService {

    private final SjdAccountGlodenBeanDetailsMapper sjdAccountGlodenBeanDetailsMapper;

    private final SjdAccountMapper sjdAccountMapper;

    @Override
    public SjdAccountDO createAccount(Long memberId) {
        //TODO 族群为规划，农场未规划
        SjdAccountDO sjdAccountDO = SjdAccountDO.builder().memberId(memberId)
                .memberLevel(MemberLevelConstants.NORMAL).goldenNum(0).integral(0).belongGroup("default")
                .createTime(new Date()).modifyTime(new Date()).build();
        sjdAccountMapper.insertSelective(sjdAccountDO);
        return sjdAccountDO;
    }

    @Override
    public SjdAccountDO findAccountByMemberId(Long memberId) {
        SjdAccountDO sjdAccountDO = sjdAccountMapper.findAccountByMemberId(memberId);
        if (ObjectUtil.isEmpty(sjdAccountDO))
            return createAccount(memberId);
        return sjdAccountDO;
    }

    @Override
    public Integer updateAccount(SjdAccountDO sjdAccountDO) {
        return sjdAccountMapper.updateAccount(sjdAccountDO);
    }
}
